package lhb.test.SC.zuul.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import lhb.test.SC.uum.model.User;


public class RouteFilter extends ZuulFilter{
	
	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user!=null){
			return false;
		}
		String uri = request.getRequestURI();
		String[] noLoginUrls= new String[]{"/uum/login"};
		for (String s : noLoginUrls) {
			if(s.equals(uri)){
				return false;
			}
		}
		return true;
	}
	
	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		System.out.println("serviceId:"+ctx.get(FilterConstants.SERVICE_ID_KEY)+",uri:"+ctx.get(FilterConstants.REQUEST_URI_KEY));
//		ctx.set(FilterConstants.SERVICE_ID_KEY,"uum");
//		ctx.set(FilterConstants.REQUEST_URI_KEY,"/onNoLog");
		return null;
	}


	@Override
	public int filterOrder() {
		return FilterConstants.RIBBON_ROUTING_FILTER_ORDER-1;
	}

	@Override
	public String filterType() {
		return FilterConstants.ROUTE_TYPE;
	}
	

}
